from win32com.client import Dispatch
import win32com.client
import os
import shutil
import zipfile

reportPath = 'C:\\Report'
outputFilePath = 'C:\\output'
pickList = 'Picklist.txt'
zipFileName = 'C:\\zip\\C0111290.PROD.CMBGP2.zip'

def ScanFolder(path, outputFilePath):
    fileList = []
    
    for item in os.listdir(path):
        filePath = os.path.join(path, item)
        
        if os.path.isfile(filePath):
            fileList.append(filePath)
        if os.path.isdir(filePath):
            ScanFolder(filePath, outputFilePath)

    if (len(fileList) > 0):
        outputFile = os.path.join(path, os.path.split(path)[1].replace(' ', '_') + '.pdf')
        destFile = os.path.join(outputFilePath, os.path.split(path)[1].replace(' ', '_') + '.pdf')

        inputFiles = filter(lambda c:c.find('Potential') > -1 and c.find('.pdf') > -1, fileList) +\
                     filter(lambda c:c.find('DetailRpt') > -1 and c.find('.pdf') > -1, fileList) +\
                     filter(lambda c:c.find('DetailRpt') > -1 and c.find('.pdf') > -1, fileList) +\
                     filter(lambda c:c.find('DetailRpt') > -1 and c.find('.pdf') > -1, fileList) +\
                     filter(lambda c:c.find('BlankRpt') > -1 and c.find('.pdf') > -1, fileList) +\
                     filter(lambda c:c.find('PowerName') > -1 and c.find('.pdf') > -1, fileList) +\
                     filter(lambda c:c.find('OverviewMap') > -1 and c.find('.pdf') > -1, fileList)
        
        if (len(inputFiles) > 0):
            MergePDF(inputFiles, outputFile)
            shutil.copyfile(outputFile, destFile)
            WriteFile(os.path.split(destFile)[1])
            
            print 'Merge at ' + outputFile
            for inputFile in inputFiles:
                if os.path.exists(inputFile):
                    os.remove(inputFile)

def MergePDF(inputFiles, outputFile):
    outputDoc = win32com.client.Dispatch('AcroExch.PDDoc')
    inputDoc = win32com.client.Dispatch('AcroExch.PDDoc')
    
    outputDoc.Create()
    
    for inFile in inputFiles:
        inputDoc.Open(inFile)
        outputDoc.InsertPages(outputDoc.GetNumPages() - 1, inputDoc, 0, inputDoc.GetNumPages(), 0)
        inputDoc.Close()

    outputDoc.Save(1, outputFile)
    outputDoc.close()

def WriteFile(message):
    fileName = os.path.join(outputFilePath, pickList)

    f = open(fileName, 'a')
    f.write(message + '\n')
    f.close()

if __name__ == "__main__":

    try:
        if not os.path.exists(outputFilePath):
            os.mkdir(outputFilePath)

        if os.path.exists(os.path.join(outputFilePath, pickList)):
            os.remove(os.path.join(outputFilePath, pickList))
           
        ScanFolder(reportPath, outputFilePath)
        WriteFile(pickList)

        toCompressFiles = []
        
        for root, dirs, files in os.walk(outputFilePath):
            for name in files:
                toCompressFiles.append(os.path.join(root,name))
                
        zf = zipfile.ZipFile(zipFileName, 'w', zipfile.zlib.DEFLATED)

        for tofile in toCompressFiles:
            zf.write(tofile)

        zf.close()

		ftp = ftplib.FTP('ftp.dmrainc.com')
		ftp.login('globex', 'freb482sax')
		print ftp.getwelcome()

		ftp.cwd('in')

		ftpSaveFile = os.path.split(zipFileName)[1]
		ftp.storbinary('STOR ' + ftpSaveFile, open(zipFileName, 'rb'), 2048)
    
		ftp.quit()
        
    except Exception as e:
        print e